refactor: unify stream caching via HybridCache#2460
Draft
j2rong4cn wants to merge 17 commits intoOpenListTeam:mainfrom
Draft
refactor: unify stream caching via HybridCache#2460j2rong4cn wants to merge 17 commits intoOpenListTeam:mainfrom
j2rong4cn wants to merge 17 commits intoOpenListTeam:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors stream buffering/caching to use a new HybridCache abstraction (memory-first with file fallback) and removes the prior mmap-based buffering path, while also introducing guarded memory allocation tied to a configurable minimum free-memory threshold.
Changes:
- Added
internal/mem(LinearMemory, GuardedMemory, HybridCache) and wired it into stream caching + downloader buffering. - Introduced
pkg/buffersection/stream abstractions (Section,StreamBuffer) and updated stream readers to use them. - Added
min_free_memoryMBconfiguration and updated bootstrap logic to derive memory/buffer limits from system memory.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/pool/pool.go | Simplifies generic pool and adds Close() for closer-based lifecycle management. |
| pkg/buffer/utils.go | Adds helpers to obtain reader/writer views for Section implementations. |
| pkg/buffer/type.go | Introduces SectionReader/SectionWriter interfaces. |
| pkg/buffer/stream.go | Adds a concurrent 1-reader/1-writer streaming buffer over a Section. |
| pkg/buffer/bytes.go | Adds Close() to Reader and introduces byteSection (Section) backed by a byte slice. |
| internal/stream/util.go | Refactors StreamSectionReader to use pooled sections and HybridCache instead of mmap/temp-file logic. |
| internal/stream/stream.go | Refactors FileStream caching/peek behavior to use HybridCache for large streams. |
| internal/stream/stream_test.go | Updates tests to exercise the new caching thresholds and behavior. |
| internal/net/request.go | Refactors downloader chunk buffering to use buffer.StreamBuffer + optional HybridCache. |
| internal/net/request_test.go | Removes unused test helpers after downloader refactor. |
| internal/mem/utils.go | Adds memory growth checks and guarded allocation wrappers. |
| internal/mem/type.go | Defines LinearMemory and GrowCheck primitives. |
| internal/mem/mem_windows.go | Implements LinearMemory via Windows VirtualAlloc. |
| internal/mem/mem_unix.go | Implements LinearMemory via mmap/mprotect on unix. |
| internal/mem/mem_other.go | Fallback LinearMemory implementation using Go slices on other platforms. |
| internal/mem/cache.go | Implements HybridCache (memory-first, file fallback) and a reader over cached sections. |
| internal/conf/var.go | Adds MinFreeMemory global var. |
| internal/conf/config.go | Adds min_free_memoryMB config field. |
| internal/bootstrap/config.go | Computes and logs MinFreeMemory, MaxBufferLimit, and MmapThreshold from system memory/config. |
| drivers/teldrive/types.go | Updates interface type name to the new StreamSectionReader. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description / 描述
🛠️ 主要改动
mmap实现为LinearMemory。min_free_memoryMB配置项。⚙️ 配置
新增配置项:
cache_threshold单位MB,使用HybridCache的阈值max_block_limit单位BM,HybridCache单次扩容限制min_free_memory单位MB,最小空闲内存阈值,可用内存小于阈值则使用文件缓存废弃配置项:
max_buffer_limitMBmmap_thresholdMBMotivation and Context / 背景
How Has This Been Tested? / 测试
go test ./internal/streamChecklist / 检查清单
我已阅读 CONTRIBUTING 文档。
go fmtor prettier.我已使用
go fmt或 prettier 格式化提交的代码。我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
我已相应更新了相关仓库(若适用)。